/*
*	ID : pallab81
* 	PROG : 10086
* 	LANG : C++
* 	2012-06-18-16.15.24 Monday
*
* 	"I have not failed, I have just found 10000 ways that won't work.
*/

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;


#define foR(i1,st,ed) for(int i1 = st , j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i1,ed) foR( i1 , 0 , ed )
#define foE(i1,st,ed) foR( i1, st, ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define line putchar_unlocked('\n');

#define getchar getchar_unlocked
#define putchar putchar_unlocked

inline void fastRead(int *a) {
    register char c=0;
    while (c<33) c=getchar();
    *a=0;
    while (c>33)
    {
        *a=*a*10+c-'0';
        c=getchar();
    }
}
inline void fastOut(int a){
    if(a){
        int v=a%10;
        fastOut(a/10);
        putchar_unlocked( (char)(v+'0') );
    }

}
inline void myOut(int a){
    if(a==0){
        putchar_unlocked('0');
    }
    else{
        fastOut(a);
    }

}
template<class T1>
inline void debug(T1 _x) {
    cout<<_x<<'\n';
}
template<class T1, class T2>
inline void debug(T1 _x,T2 _y) {
    cout<<_x<<' '<<_y<<'\n';
}
template<class T1, class T2, class T3>
inline void debug(T1 _x,T2 _y,T3 _z) {
    cout<<_x<<' '<<_y<<' '<<_z<<'\n';
}
template<class T1, class T2, class T3, class T4>
inline void debug(T1 _x,T2 _y,T3 _z,T4 _zz) {
    cout<<_x<<' '<<_y<<' '<<_z<<' '<<_zz<<'\n';
}
template< class T1>
inline void debug(T1 _array,int _size) {
    cout<<"[";
    for (int i=0; i<_size; ++i) {
        cout<<' '<<_array[i];
    }
    puts(" ]");
}
inline bool CI(int &_x) {
    return scanf("%d",&_x)==1;
}
inline bool CI(int &_x, int &_y) {
    return CI(_x)&&CI(_y) ;
}
inline bool CI(int &_x, int &_y, int &_z) {
    return CI(_x)&&CI(_y)&&CI(_z) ;
}
inline bool CI(int &_x, int &_y, int &_z, int &_zz) {
    return CI(_x)&&CI(_y)&&CI(_z)&&CI(_zz) ;
}
inline void wait( double seconds ){
    double endtime = clock()+( seconds* CLOCKS_PER_SEC );
    while( clock() < endtime ){
        ;
    }
}
namespace In{
    int T1,T2;

    int N;
    int g[30][30][2];
    int c[30];
    inline void read1(){
        fastRead(&N);
        fo(i,N){
            fastRead(&c[i]);
            fo(k,2){
                fo(j,c[i]){
                    fastRead(&g[i][j][k]);
                }
            }
        }
    }
}

namespace algo{
const int inf = 1023456789;
int dp[30][30][30];
bool vv[30][30][30];
    int go(int at,int at1, int at2){
        if(at1==0&&at2==0)return 0;

        if(at>=In::N)return inf;
        if( vv[at][at1][at2] ) return dp[at][at1][at2];
        vv[at][at1][at2]=true;
        int best=inf;
        for(int i=0;i<=In::c[at];++i){
            int side1 = i;
            int side2 = In::c[at]-side1;
            if( at1>=side1 && at2>=side2 ){
                int c1= (side1 ? In::g[at][side1-1][0] : 0 ) ;
                int c2= (side2 ? In::g[at][side2-1][1] : 0 ) ;

                int v=go(at+1,at1-side1,at2-side2)+c1+c2 ;
                best=min(best,v);
            }
        }
        return dp[at][at1][at2]=best;
    }

    void bk(int at, int at1, int at2,int ret){
        if(at1==0&&at2==0)return ;
        for(int i=0;i<=In::c[at];++i){
            int side1 = i;
            int side2 = In::c[at]-side1;
            if( at1>=side1 && at2>=side2 ){
                int c1= (side1 ? In::g[at][side1-1][0] : 0 ) ;
                int c2= (side2 ? In::g[at][side2-1][1] : 0 ) ;

                int v=go(at+1,at1-side1,at2-side2)+c1+c2 ;
                if(v==ret){
                    if(at)putchar_unlocked(' ');
                    myOut(side1);
                    bk(at+1,at1-side1,at2-side2, ret-(c1+c2));
                    return ;
                }
            }
        }

    }
    inline void solve(){
        SET(vv,false);
        int var=go(0,In::T1,In::T2);
        myOut(var);line;
        bk(0,In::T1,In::T2,var);
        line;
    }
}
#define FILE
int main(){
	#ifdef FILE
	freopen("/home/xerxes/Documents/10086/in","rt",stdin);
	#endif
    while( true ){
        fastRead(&In::T1);
        fastRead(&In::T2);
        if(In::T1==0&&In::T2==0)break;
        In::read1();
        algo::solve();
        line;
    }


return 0;
}
